package org.daidai.abstrusewriting.homepage.classify.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.fragments.BaseFragment;
import org.daidai.abstrusewriting.homepage.adapters.CommonFragmentAdapter;
import org.daidai.abstrusewriting.homepage.classify.client.ClientAPI;
import org.daidai.abstrusewriting.homepage.classify.fragments.ClassifyItemFragment;
import org.daidai.abstrusewriting.homepage.classify.model.ClassifyItem;
import org.daidai.abstrusewriting.homepage.classify.model.TabItem;
import org.daidai.abstrusewriting.homepage.classify.model.TabItemList;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class ClassifyItemActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
    private static final String TAG = "ClassifyItemActivity";
    private Toolbar mToolbar;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify_item);
        translucentStatusBar();
        if(!(EventBus.getDefault().isRegistered(this))){
            EventBus.getDefault().register(this);
        }

        mToolbar = (Toolbar) findViewById(R.id.classify_item_activity_toolbar);
        mTabLayout = (TabLayout) findViewById(R.id.classify_item_activity_tablayout);
        mViewPager = (ViewPager) findViewById(R.id.classify_item_activity_viewpager);


        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }

        Intent intent = getIntent();
        ClassifyItem item = intent.getParcelableExtra("item");
        //Log.d(TAG, "onCreate: "+item.toString());

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(item.getTitle());
        }


        ClientAPI.getTabItemList(item.getId());
        ClientAPI.getSpecialDetail(1,1,20);

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetTabItemList(TabItemList tabItems) {

        //Toast.makeText(this, tabItems.toString(), Toast.LENGTH_SHORT).show();

        //Log.d(TAG, "onGetTabItemList: "+tabItems);



        TabLayout.Tab tab = mTabLayout.newTab();


        tab.setText("推荐");
        mTabLayout.addTab(tab);

        List<BaseFragment> fragments=new ArrayList<>();

        fragments.add(new ClassifyItemFragment());

        for (int i = 0; i < tabItems.getTabItems().size(); i++) {
            TabItem tabItem = tabItems.getTabItems().get(i);
            tab = mTabLayout.newTab();
            tab.setText(tabItem.getKeywordName());
            mTabLayout.addTab(tab);
            ClassifyItemFragment fragment = new ClassifyItemFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("categoryId",tabItem.getCategoryId());
            bundle.putLong("keywordId",tabItem.getKeywordId());
            fragment.setArguments(bundle);

            fragments.add(fragment);
        }
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);


        CommonFragmentAdapter mAdapter=new CommonFragmentAdapter(
                getSupportFragmentManager(),
                fragments
        );

        mViewPager.setAdapter(mAdapter);
        mTabLayout.addOnTabSelectedListener(this);
        mViewPager.addOnPageChangeListener(
                new TabLayout.TabLayoutOnPageChangeListener(mTabLayout)
        );



    }



    /**
     * 手机状态栏透明化:代码实现（还可以在style中设置）
     *
     *
     */
    private void translucentStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
